package impls.agents;

import impls.classes.Constants;
import impls.classes.Position;
import interfaces.agents.Actor;
import jade.core.AID;
import jade.core.Agent;
import jade.lang.acl.ACLMessage;

import java.util.ArrayList;
import java.util.Random;

/**
 * parent class for agents shown on screen (car and station)
 * @author thailow
 *
 */
public class ActorAgent extends Agent implements Actor {

	private static final long serialVersionUID = 7525811491395075981L;

	protected String TYPE = "actor";

	protected Position pos;

	protected AID graphicsAID;
	
	protected ArrayList<ACLMessage> msgs = new ArrayList<ACLMessage>();

	/**
	 * basic setup for creating initial position
	 */
	public void setup() {
		Random r = new Random();
		this.pos = new Position(r.nextInt(100), r.nextInt(100));
	}

	/**
	 * sends current position to graphics agent
	 */
	public void sendPos(String include[]) {
		StringBuffer appendBuf = new StringBuffer();
		for(String s : include) {
			appendBuf.append(s + ",");
		}
		int length = appendBuf.length();
		String append = length > 0 ? appendBuf.substring(0, appendBuf.length() - 1) : "";
		ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
		msg.setConversationId(Constants.DRAW);
		msg.setContent(getAID() + "," + TYPE + "," + pos.getX() + ","
				+ pos.getY()+ "," + append);
		msg.addReceiver(graphicsAID);
		send(msg);
	}
	/**
	 * string output followed by ":"
	 */
	public String toString() {
		return TYPE + " " + this.getLocalName() + ": ";
	}
	
	/**
	 * sends current message to graphics agent
	 */
	public void sendMessage(String message) {
		ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
		msg.setConversationId(Constants.MESSAGE);
		msg.setContent(message);
		msg.addReceiver(graphicsAID);
		send(msg);
	}
	
}
